﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using saigonbook.Biz;
using System.Data;

namespace saigonbook.Dal
{
    class DAL_tonkho
    {
        public static List<BIZ_tonkho> Select_tonkho()
        {
            DataProvider db = new DataProvider();
            DataTable dt = db.GetData("Select * from TONKHO");
            List<BIZ_tonkho> list = new List<BIZ_tonkho>();
            foreach (DataRow row in dt.Rows)
            {
                BIZ_tonkho tonkho = new BIZ_tonkho((DateTime)row[0], DAL_sach.GetSachById((int)row[1]), (int)row[2]);
                list.Add(tonkho);
            }
            return list;
        }

        public static List<BIZ_tonkho> Select_tonkho(BIZ_sach sach)
        {
            DataProvider db = new DataProvider();
            DataTable dt = db.GetData("Select * from TONKHO where masach=N'"+ sach.masach +"'");
            List<BIZ_tonkho> list = new List<BIZ_tonkho>();
            foreach (DataRow row in dt.Rows)
            {
                BIZ_tonkho tonkho = new BIZ_tonkho((DateTime)row[0], DAL_sach.GetSachById((int)row[1]), (int)row[2]);
                list.Add(tonkho);
            }
            return list;
        }

        public static void Them_tonkho(BIZ_tonkho dto)
        {
            DataProvider db = new DataProvider();
            db.ExecuteQuery("Insert into tonkho(ngay,masach,soluong) VALUES (N'" + dto.ngay.Date.ToString("MM-dd-yyyy") + "',N'" + dto.sach.masach + "',N'" + dto.soluong + "')");
        }

        public static void Capnhat_tonkho(BIZ_tonkho dto)
        {
            DataProvider db = new DataProvider();
            db.ExecuteQuery("Update tonkho set soluong=N'" + dto.soluong + "' where masach= '" + dto.sach.masach + "'and ngay='" + dto.ngay.Date.ToString("MM-dd-yyyy") + "'");
        }
    }
}
